<?php

namespace app\admin\model;

use think\Model;
use think\Db;

class ResourcesModel extends Model {

    public $results = array('status' => 'error', 'msg' => '', 'data' => '');

    //分类列表
    function option_list() {
        $rs = Db::table('option_list')
                ->select();
        return $rs;
    }

    function option_info($id) {
        $where['id'] = $id;
        return Db::table('option_list')->where($where)->find();
    }

    function change_option($data, $id) {
        $where['id'] = $id;
        $rs = Db::table('option_list')->where($where)->update($data);
        return $rs;
    }

    function add_option($data) {
        $rs = Db::table('option_list')->insert($data);
        return $rs;
    }

    function del_option($id) {
        $where['id'] = $id;
        return Db::table('option_list')->delete($where);
    }

    /**
     *     //资源列表
     * @param type $key  搜索关键字
     * @param type $type  分类类型
     * @return type
     */
    function res_list($key = "", $type = "") {
        //分页带条件
        $pageParam['query']['key'] = $key;
        $pageParam['query']['type'] = $type;
        if ($key) {
            $where['title'] = ['like', "%$key%"];
        }
        if ($type) {
            $where['type'] = $type;
        }
        $rs = Db::table('acticle')
                ->where($where)
                ->order("create_time desc")
                ->paginate(10, FALSE, $pageParam);
        if (!$rs) {
            return $this->results;
        }
        $this->results['status'] = 'success';
        $this->results['data'] = $rs;
        return $this->results;
    }

    /**
     * 上线 /下线
     * @param type $res_id
     * @param type $state
     */
    function banned($res_id, $state) {
        //停用1  启用2
        if ($state == 1) {
            $up['state'] = "0";
        } else {
            $up['state'] = "1";
        }
        return Db::table('acticle')->where('id', $res_id)->update($up);
    }

    //资源信息
    function res_info($res_id) {
        $rs = Db::table('acticle')
                ->where("id", $res_id)
                ->find();
        return $rs;
    }

    function change_info($data) {
        return Db::table('acticle')->where('id', $data['id'])->update($data);
    }

    //分类列表
    function class_list() {
        return Db::table('option_list')->select();
    }

    //所有能上线的当前分类id
    function all_id($type) {
        $where['state'] = 1;
        $where['type'] = $type;
        $rs = Db::table('acticle')->field("id")
                ->where($where)
                ->order("create_time desc")
                ->select();
        return $rs;
    }

    //获取多个资源信息
    function get_more_info($id_list) {
        $rs = Db::table('acticle')
                ->where('id', 'IN', $id_list)
                ->order("create_time desc")
                ->select();
        return $rs;
    }

}
